package cn.saltybe.analysis.crawler.wbtc.wholeRentingHouse;

import cn.saltybe.analysis.crawler.wbtc.common.WbtcHouseImage;
import cn.saltybe.analysis.entity.data.RentingHouse;
import cn.saltybe.analysis.service.RentingHouseServiceImpl;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component("wbtcWholeRentingHousePipeline")
@PipelineName("wbtcWholeRentingHousePipeline")
public class WbtcWholeRentingHousePipeline implements Pipeline<WbtcWholeRentingHouse> {
    private static RentingHouseServiceImpl rentingHouseService;

    @Autowired
    public void setRentingHouseService(RentingHouseServiceImpl rentingHouseService) {
        this.rentingHouseService = rentingHouseService;
    }

    @Override
    public void process(WbtcWholeRentingHouse wbtcWholeRentingHouse) {
        try {
            String typeId = "wbtc" + wbtcWholeRentingHouse.getId();
            HttpRequest currentRequest = wbtcWholeRentingHouse.getRequest();

            String province = currentRequest.getParameter("province");
            String city = currentRequest.getParameter("city");

            RentingHouse wholeRentingHouse = new RentingHouse();
            wholeRentingHouse.setCity(city);
            wholeRentingHouse.setDistrict(wbtcWholeRentingHouse.getArea());
            wholeRentingHouse.setSource(currentRequest.getUrl());
            System.out.println(wbtcWholeRentingHouse.getPrice());
            wholeRentingHouse.setPrice(Integer.parseInt(wbtcWholeRentingHouse.getPrice()));
            wholeRentingHouse.setTitle(wbtcWholeRentingHouse.getTitle());
            wholeRentingHouse.setAddress(wbtcWholeRentingHouse.getAddress());
            for (WbtcHouseImage i : wbtcWholeRentingHouse.getImage()) {
                if (i.getImg2().length() > 0) {
                    wholeRentingHouse.setImage(i.getImg2());
                    break;
                }
            }
            wholeRentingHouse.setType_id(typeId);
            wholeRentingHouse.setRent_type("whole");
            // 解析房屋面积
            Pattern squarePattern = Pattern.compile(".*\\s+(\\d+)\\s*平\\s*.*");
            Matcher squareMatcher = squarePattern.matcher(wbtcWholeRentingHouse.getType());
            if (squareMatcher.find()) {
                wholeRentingHouse.setSquare(Double.parseDouble(squareMatcher.group(1)));
            } else {
                System.out.println("解析房屋面积时失败！");
                System.out.println("页面链接\t" + currentRequest.getUrl());
                System.out.println("房屋面积字符串\t" + wbtcWholeRentingHouse.getType());
                return;
            }

            try {
                if (rentingHouseService.has(typeId)) {
                    System.out.println("更新旧条目");
                    rentingHouseService.update(wholeRentingHouse);
                    System.out.println("旧条目已更新");
                } else {
                    System.out.println("插入新条目");
                    rentingHouseService.insert(wholeRentingHouse);
                    System.out.println("新条目已入库");
                }
                System.out.println("所在地\t" + province + " " + city + " " + wholeRentingHouse.getDistrict());
                System.out.println("类别ID\t" + typeId);
                System.out.println("URL\t" + currentRequest.getUrl());
                System.out.println("标题\t" + wholeRentingHouse.getTitle());
                System.out.println("地址\t" + wholeRentingHouse.getAddress());
                System.out.println("房屋面积售价\t" + wholeRentingHouse.getSquare() + "㎡" + "\t" + wholeRentingHouse.getPrice() + "元/月");
            } catch (Exception e) {
                System.out.println("条目置入数据库失败");
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
